package ar.edu.utn.frgp.banco.dao.Implements;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import ar.edu.utn.frgp.banco.dao.Interfaces.IntServicios;
import ar.edu.utn.frgp.banco.dao.connection.ImpConectionHandler;
import ar.edu.utn.frgp.banco.domain.Servicio;
import ar.edu.utn.frgp.banco.exceptions.DataAccessException;

public class ImpServicios implements IntServicios {

	@Override
	public Servicio TraeServivio(Integer id_servicio)
			throws DataAccessException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		ImpConectionHandler handler = new ImpConectionHandler();
		Connection cn = handler.getConnection();
		Servicio serv = null;
		
		try {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append("Select id_servicio,description,montofijo, ");
			stringBuilder.append("fechainicio, fechafinal ");
			stringBuilder.append("from servicios ");
			stringBuilder.append("WHERE id_servicio = ?");

			ps = cn.prepareStatement(stringBuilder.toString());
			ps.setInt(1, id_servicio);
			
			rs = ps.executeQuery();
			
			while (rs.next()) {
				serv = new Servicio();
				
				serv.setId_servicio(rs.getInt("id_servicio"));
				serv.setDescripcion(rs.getString("description"));
				serv.setMontoFijo(rs.getDouble("montofijo"));
				serv.setFechainici(rs.getDate("fechainicio"));
				serv.setFechafinal(rs.getDate("fechafinal"));
				
			}
			
		} catch (SQLException e) {
			throw new DataAccessException(e);
		} finally {
			handler.closeResources(rs, ps, cn);
		}
		return serv;
	}

	@Override
	public List<Servicio> TraeServicios() throws DataAccessException {
		// TODO Auto-generated method stub
		Statement st = null;
		ResultSet rs = null;
		
		List<Servicio> servicios = null;
		
		ImpConectionHandler handler = new ImpConectionHandler();
		Connection cn = handler.getConnection();
		
		try {
			st = cn.createStatement();
			
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append("Select id_servicio,description,montofijo, ");
			stringBuilder.append("fechainicio, fechafinal ");
			stringBuilder.append("from servicios ");

			rs = st.executeQuery(stringBuilder.toString());
			
			if (rs.isBeforeFirst()) {
				
				servicios = new ArrayList<Servicio>();
				
				Servicio serv = null;
				while (rs.next()) {
					serv = new Servicio();

					serv.setId_servicio(rs.getInt("id_servicio"));
					serv.setDescripcion(rs.getString("description"));
					serv.setMontoFijo(rs.getDouble("montofijo"));
					serv.setFechainici(rs.getDate("fechainicio"));
					serv.setFechafinal(rs.getDate("fechafinal"));

					servicios.add(serv);
				}

			}
			
		} catch (SQLException e) {
			throw new DataAccessException(e);
		} finally {
			handler.closeResources(rs, st, cn);
		}
		
		return servicios;
	}

}
